package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.CommentInfo;
import com.fun_ordering.service.CommentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论信息表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/commentInfo")
public class CommentInfoController {

	@Autowired
	private CommentInfoService commentInfoService;

	/**
	 * 获取所有评论信息
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<CommentInfo> findAll(){
		return commentInfoService.findAll();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return commentInfoService.findPage(page, rows);
	}

	/**
	 * 创建新评论
	 * @param commentInfo
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody CommentInfo commentInfo){
		try {
			commentInfoService.add(commentInfo);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 更新评论
	 * @param commentInfo
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody CommentInfo commentInfo){
		try {
			commentInfoService.update(commentInfo);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 根据评论ID获取评论
	 * @param commentId
	 * @return
	 */
	@RequestMapping("/findOne")
	public CommentInfo findOne(Integer commentId){
		return commentInfoService.findOne(commentId);
	}

	/**
	 * 批量删除 评论
	 * @param commentIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] commentIds){
		try {
			commentInfoService.delete(commentIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param commentInfo
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody CommentInfo commentInfo, int page, int rows  ){
		return commentInfoService.findPage(commentInfo, page, rows);
	}

}
